<?php

namespace App\Http\Controllers\Admin;

use App\Model\CategoryModel;
use App\Model\UserModel;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;


class CategoryController extends CommonController
{
    //
    public function index(){

        $data =  CategoryModel::all();

        $list =  self::getTree($data,0);

        return view('admin.category.list',compact('list'));
    }

    // 获取树状结构
    private function getTree($data = array() , $id = 0,$level = 0){

        $list = array();
        foreach ($data as $k => $v) {
            if($v['parent_id'] == $id){
                $v['level'] = $level;
                $v['html'] = str_repeat("--",($level*3));
                $list[$k] = $v;
                $list = array_merge($list,$this->getTree($data,$v['cate_id'],$level+1));
            }
        }
        return $list;
    }

    // 添加分类
    public function  create(){

        $get = Input::get();
        $errors = array();
        if($get){
          $errors = $get;
        }
        $list = CategoryModel::all();
        return view("admin.category.add_cate")->with('errors',$errors)->with('list',$list);
    }

    // 添加数据
    public function store()
    {
      $input =  Input::except('_token');

      $rules = array(
         'cate_name'=>'required',
      );

      $message = array(

          'cate_name.required'=>'分类名称不能为空!',
      );

      $validate  = Validator::make($input,$rules,$message);
      if($validate->passes()){
      $newLine = CategoryModel::create($input);
      if($newLine){
          $success_msg = "添加成功";
          return redirect('Admin/category')->with('success_msg',$success_msg);
      }else{
          $err[] = '添加出错，请稍后再试试!';
          return redirect('Admin/category/create'.$err);
      }
      }else{

        $err = '?';
          foreach ($validate->errors()->all() as $k => $v){
          $err .= "err".$k."=".$v."&";
        }
        return redirect('Admin/category/create'.$err);
        }

    }


    public function destroy($id){

       $pid = CategoryModel::where('cate_id',$id)->first()->parent_id;
       $del = CategoryModel::where('cate_id',$id)->delete();

       if($del){
           CategoryModel::where('parent_id',$id)->update(['parent_id'=>$pid]);
           return array('code'=>0,'msg'=>'删除成功');
       }else{
           return array('code'=>1001,'msg'=>'删除失败');
       }
    }

    public function show(){

    }

    //编辑数据
    public function edit($id)
    {
      $data = CategoryModel::where('cate_id',$id)->first();
      $allCates = CategoryModel::all();
      $child_ids = $this->cates($id,$allCates);
      $list = CategoryModel::whereNotIn('cate_id',$child_ids)->get();
      return view('admin.category.edit_cate',compact('data','list'));
    }

    // 递归查找自分类的id
    private function cates($id = 0,$data=array()){

        $arr = array();
        foreach ($data as $k => $v)
        {
            if($v['parent_id']==$id){
              $arr[] = $v->cate_id;
              $arr = array_merge($arr,$this->cates($v->cate_id,$data));
            }
        }
        return $arr;
    }

   // 修改数据
   public function update($id){
      $newData = Input::except("_token","_method");
      $if = CategoryModel::where('cate_id',$id)->update($newData);

      if($if){
          return redirect("Admin/category");
      }else{
          return back()->withErrors('err',"修改失败，请稍后再试!");
      }
    }
}

